package com.woniu.mgr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.mgr.model.Account;
import com.woniu.mgr.model.Coach;
import com.woniu.mgr.model.CoachApply;
import com.woniu.mgr.dao.CoachApplyDao;
import com.woniu.mgr.service.AccountService;
import com.woniu.mgr.service.CoachApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.mgr.service.CoachService;
import com.woniu.mgr.util.BeanCopyUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 教练登记申请表 服务实现类
 * </p>
 *
 * @author Silas
 * @since 2023-02-15
 */
@Service
public class CoachApplyServiceImpl extends ServiceImpl<CoachApplyDao, CoachApply> implements CoachApplyService {
    @Resource
    private CoachApplyDao coachApplyDao;
    @Resource
    private CoachService coachService;
    @Resource
    private AccountService accountService;

    @Override
    public void successBeCoach(Integer coachApplyId) {

        CoachApply coachApply = getById(coachApplyId);
        coachApply.setState("通过");
        updateById(coachApply);
        Coach coach = BeanCopyUtil.copyObject(coachApply, Coach.class);
        coachService.beCoach(coach);
        Account account = accountService.getById(coachApply.getAccountId());
        account.setType("教练");
        accountService.updateById(account);
    }

    @Override
    public IPage<CoachApply> query(String condition, Integer current, Integer size) {
        IPage<CoachApply> page = new Page<>(current == null ? 1 : current, size == null ? 4 : size);
        QueryWrapper<CoachApply> coachApplyQueryWrapper = new QueryWrapper<CoachApply>();
        if ("".equals(condition) || condition == null) {
            IPage<CoachApply> iPage = coachApplyDao.selectPage(page, coachApplyQueryWrapper);
            return iPage;
        } else {
            coachApplyQueryWrapper.eq("no", condition)
                    .or().eq("realname", condition)
                    .or().eq("phone", condition)
                    .or().eq("venue_name", condition);
            IPage<CoachApply> iPage = coachApplyDao.selectPage(page, coachApplyQueryWrapper);
            return iPage;
        }


    }

    @Override
    public void refuse(String message, Integer coachApplyId) {
        CoachApply coachApply = getById(coachApplyId);
        coachApply.setState("驳回");
        coachApply.setMessage(message);
        save(coachApply);
    }

}
